package io.renren.modules.sys.controller;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.Arrays;
import java.util.Map;

import io.renren.modules.sys.service.SysCaptchaService;
import org.apache.commons.io.IOUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import io.renren.modules.upk.entity.UpkBrandEntity;
import io.renren.modules.upk.service.UpkBrandService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;

import javax.imageio.ImageIO;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;


/**
 * 商品品牌表，保存商品品牌信息。
 *
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-05-29 20:29:24
 */
@RestController
@RequestMapping("generator/upkbrand")
public class UpkBrandController {
    @Autowired
    private UpkBrandService upkBrandService;
    @Autowired
    private SysCaptchaService sysCaptchaService;
    @GetMapping("/test")
    public void captcha(HttpServletResponse response, String uuid)throws ServletException, IOException {
        response.setHeader("Cache-Control", "no-store, no-cache");
        response.setContentType("image/jpeg");

        //获取图片验证码
        BufferedImage image = sysCaptchaService.getCaptcha(uuid);

        ServletOutputStream out = response.getOutputStream();
        ImageIO.write(image, "jpg", out);
        IOUtils.closeQuietly(out);
    }

    /**
     * 列表
     */
//    @RequestMapping("/list")
    @GetMapping("/list")
//    @RequiresPermissions("upk:upkbrand:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = upkBrandService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("upk:upkbrand:info")
    public R info(@PathVariable("id") Long id){
			UpkBrandEntity upkBrand = upkBrandService.selectById(id);

        return R.ok().put("upkBrand", upkBrand);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("upk:upkbrand:save")
    public R save(@RequestBody UpkBrandEntity upkBrand){
			upkBrandService.insert(upkBrand);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("upk:upkbrand:update")
    public R update(@RequestBody UpkBrandEntity upkBrand){
			upkBrandService.updateById(upkBrand);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("upk:upkbrand:delete")
    public R delete(@RequestBody Long[] ids){
			upkBrandService.deleteBatchIds(Arrays.asList(ids));

        return R.ok();
    }

}
